package com.tb.ClassSchedule;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by guich on 2016/8/8.
 */
public class TimeManager {

//    private static int currentWeek;
//    private static int currentWeekday;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    private static String numberString;
    private static TextView text;
//    private static boolean isNegative;



    public static  void setCurrentWeek(int currentWeek){
        Date date = new Date();
        try {
            //抹去小时分钟秒钟
            date = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if(dayOfWeek == 0)
            dayOfWeek = 7;

        //本周星期一的日期
        calendar.add(Calendar.DATE,(-1) * (dayOfWeek - 1));
        //第一个星期的星期一日期
        if(currentWeek < 0){
            currentWeek = currentWeek + 1;
        }
        calendar.add(Calendar.DATE,(-1) * (currentWeek - 1) * 7);

        date = calendar.getTime();

        saveTime(date);
    }

    public static void setCurrentWeekDialog(){
        AlertDialog.Builder builder   = new AlertDialog.Builder(MainActivity.main);

        builder.setTitle("请输入当前周");
        View view = MainActivity.main.getLayoutInflater().inflate(R.layout.number_slector,null);
        builder.setView(view);

        numberString = "";
        text    = (TextView) view.findViewById(R.id.inputed_text);
        Button         button0 = (Button) view.findViewById(R.id.button0);
        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "0";
                text.setText(numberString);
            }
        });
        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "1";
                text.setText(numberString);
            }
        });
        Button button2 = (Button) view.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "2";
                text.setText(numberString);
            }
        });
        Button button3 = (Button) view.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "3";
                text.setText(numberString);
            }
        });
        Button button4 = (Button) view.findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "4";
                text.setText(numberString);
            }
        });
        Button button5 = (Button) view.findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "5";
                text.setText(numberString);
            }
        });
        Button button6 = (Button) view.findViewById(R.id.button6);
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "6";
                text.setText(numberString);
            }
        });
        Button button7 = (Button) view.findViewById(R.id.button7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "7";
                text.setText(numberString);
            }
        });
        Button button8 = (Button) view.findViewById(R.id.button8);
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "8";
                text.setText(numberString);
            }
        });
        Button button9 = (Button) view.findViewById(R.id.button9);
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberString += "9";
                text.setText(numberString);
            }
        });
        Button buttonFlag = (Button) view.findViewById(R.id.button_flag);
        buttonFlag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(numberString.length() == 0) {
                    numberString = "-";
                }else{
                    char flag = numberString.charAt(0);
                    if (flag == '-') {
                        numberString = numberString.substring(1);
                    } else {
                        numberString = "-" + numberString;
                    }
                }
                text.setText(numberString);
            }
        });
        Button buttonDelete = (Button) view.findViewById(R.id.button_delete);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(numberString.length() > 0){
                    numberString = numberString.substring(0,numberString.length()-1);
                    text.setText(numberString);
                }

            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(numberString != ""){
                    int currentWeek = Integer.parseInt(text.getText().toString());
                    setCurrentWeek(currentWeek);

                    if(currentWeek > 21){
                        currentWeek = 21;
                    }else if(currentWeek < 0){
                        currentWeek = 0;
                    }
                    MainActivity.main.currentWeek = currentWeek;
                    MainActivity.main.selectedWeek = currentWeek;
                    MainActivity.main.draw();
                }

            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();

    }

    public static int getCurrentWeek(){
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(loadTime());
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(new Date());
        int days = (int) ((endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis())
                        / (1000 * 60 * 60 * 24));
        int currentWeek = days / 7 ;
        if(currentWeek > 0){
            currentWeek = currentWeek + 1;
        }else if(currentWeek < 0){
            currentWeek = currentWeek - 1;
        }else{
            if(currentWeek == 0){
                setCurrentWeek(0);
            }
        }

        return currentWeek;
    }

    public static int getCurrentWeekday(){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        //Calendar.DAY_OF_WEEK返回1~7，1代表周日，7代表周六
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if(dayOfWeek == 0)
            dayOfWeek = 7;
        return dayOfWeek;
    }


    private static void saveTime(Date date){
        Log.d("tb",sdf.format(date));
        SharedPreferences.Editor editor = MainActivity.sharedPreferences.edit();
        editor.putString("first_day",sdf.format(date));
        editor.commit();
        editor.clear();
    }

    private static Date loadTime(){
        Date date;
        String dateString = MainActivity.sharedPreferences.getString("first_day","");
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            date = new Date();
            e.printStackTrace();
        }

        return date;
    }

}
